<template>
  <div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="150px">
      <!--添加门店-->
      <div class="common-form">商城设置</div>
      <el-form-item label="配送方式">
        <el-checkbox-group v-model="form.deliveryType">
          <el-checkbox v-for="(item, index) in all_type" :label="item.value" :key="index">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
        <div class="tips">注：配送方式至少选择一个</div>
      </el-form-item>
      <div class="common-form">h5注册设置</div>
      <el-form-item label="是否开启短信验证" prop="smsOpen">
        <el-checkbox v-model="form.smsOpen">是否开启短信验证</el-checkbox>
        <div class="tips">用户注册是否开启短信验证</div>
      </el-form-item>
      <div class="common-form">微信注册设置</div>
      <el-form-item label="是否开启微信授权" prop="wxOpen">
        <el-checkbox v-model="form.wxOpen">是否开启微信授权</el-checkbox>
        <div class="tips">用户登录是否开启微信授权</div>
      </el-form-item>
      <el-form-item label="是否开启微信手机号绑定" prop="wxBinding">
        <el-checkbox v-model="form.wxBinding">是否开启微信手机号绑定</el-checkbox>
        <div class="tips">是否开启微信内置手机号绑定</div>
      </el-form-item>
      <div class="common-form">微信小程序发货设置</div>
      <el-form-item label="小程序发货" prop="isSendWx">
        <el-checkbox v-model="form.isSendWx">是否自动向微信小程序发货</el-checkbox>
        <div class="tips">注：请确认微信小程序有发货信息管理权限再开启</div>
      </el-form-item>
      <div class="common-form">日志记录</div>
      <el-form-item label="是否记录查询日志" prop="customer">
        <el-checkbox v-model="form.isGetLog">是否记录查询日志</el-checkbox>
        <div class="tips">如果记录，日志量会有点大</div>
      </el-form-item>
      <!--   <div class="common-form">物流查询api</div>
      <el-form-item
        label="快递100 Customer"
        :rules="[{ required: true, message: ' ' }]"
        prop="kuaiDi100.customer"
      >
        <el-input
          v-model="form.kuaiDi100.customer"
          placeholder=""
          class="max-w460"
        ></el-input>
        <div class="tips">
          用于查询物流信息,<el-link
            :underline="false"
            href="https://www.kuaidi100.com/openapi/"
            target="_blank"
            type="primary"
            >快递100申请</el-link
          >
        </div>
      </el-form-item>
      <el-form-item label="快递100 secret" prop="kuaiDi100.secret">
        <el-input
          v-model="form.kuaiDi100.secret"
          placeholder=""
          class="max-w460"
        ></el-input>
        <div class="tips">
          用于电子面单下单,<el-link
            :underline="false"
            href="https://www.kuaidi100.com/openapi/"
            target="_blank"
            type="primary"
            >快递100申请</el-link
          >
        </div>
      </el-form-item>
      <el-form-item
        label="快递100 Key"
        :rules="[{ required: true, message: ' ' }]"
        prop="kuaiDi100.key"
      >
        <el-input
          v-model="form.kuaiDi100.key"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item> -->
      <div class="common-form">政策协议</div>
      <el-tabs v-model="activeTab" type="card">
        <el-tab-pane label="服务协议" name="service"></el-tab-pane>
        <el-tab-pane label="隐私政策" name="privacy"></el-tab-pane>
        <el-tab-pane label="充值政策" name="charge"></el-tab-pane>
        <el-tab-pane label="买前必读" name="needred"></el-tab-pane>
      </el-tabs>
      <el-form-item label="服务协议" v-show="activeTab == 'service'">
        <div class="edit_container">
          <Uediter :text="form.policy.service" :config="ueditor.config1" :editorid="editorid1" id="serviceEditor"
            ref="serviceEditor" @contentChange="contentChangeFuncs"></Uediter>
        </div>
        <div class="tips">
          <el-link>进入app和登录、注册页面显示</el-link>
        </div>
      </el-form-item>
      <el-form-item label="隐私政策" v-show="activeTab == 'privacy'">
        <Uediter :text="form.policy.privacy" :config="ueditor.config" id="privacyEditor" ref="privacyEditor"
          :editorid="editorid2" @contentChange="contentChangeFunc"></Uediter>
      </el-form-item>
      <el-form-item label="充值政策" v-show="activeTab == 'charge'">
        <Uediter :text="form.policy.charge" :config="ueditor.config3" id="chargeEditor" ref="chargeEditor"
          :editorid="editorid3" @contentChange="contentChangechargeFunc"></Uediter>
      </el-form-item>
      <el-form-item label="买前必读" v-show="activeTab == 'needred'">
        <Uediter :text="form.policy.needred" :config="ueditor.config4" id="needredEditor" ref="needredEditor"
          :editorid="editorid4" @contentChange="contentChangeReadFunc"></Uediter>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" @click="onSubmit" :loading="loading" icon="CircleCheck">提交</el-button>
      </div>
    </el-form>
    <!--上传图片-->
    <Upload v-if="isupload" :isupload="isupload" :config="{ total: 3 }" @returnImgs="returnImgsFunc"></Upload>
  </div>
</template>

<script>
import Uediter from "@/components/UE.vue";
import SettingApi from "@/api/setting.js";
import Upload from "@/components/file/Upload.vue";
import { formatModel } from "@/utils/base.js";
export default {
  components: {
    Upload,
    Uediter,
  },
  data() {
    return {
      ueditor: {
        text: "",
        config: {
          initialFrameWidth: 600,
          initialFrameHeight: 500,
        },
        config1: {
          initialFrameWidth: 600,
          initialFrameHeight: 500,
        },
        config3: {
          initialFrameWidth: 600,
          initialFrameHeight: 500,
        },
        config4: {
          initialFrameWidth: 600,
          initialFrameHeight: 500,
        }
      },
      /*是否正在加载*/
      loading: false,
      /*form表单数据*/
      form: {
        name: "",
        kuaiDi100: {
          customer: "",
          key: "",
        },
        deliveryType: [],
        isGetLog: 0,
        policy: {
          service: "",
          privacy: "",
          charge: '',
          needred: ''
        },
      },
      all_type: [],
      delivery_type: [],
      /*是否打开图片选择*/
      isupload: false,
      editorid1: "editor-demo-01",
      editorid2: "editor-demo-02",
      editorid3: "editor-demo-03",
      editorid4: 'editor-demo-04',
      activeTab: "service",
    };
  },
  created() {
    this.getParams();
    //this.form.policy.privacy = localStorage.getItem("contens");
    //this.form.policy.service = localStorage.getItem("contens1");
    //  this.form.policy.charge = localStorage.getItem("charge");
    // this.form.policy.needred = localStorage.getItem("needred");
  },
  mounted() {

  },
  computed: {
    /*获取当前语言*/
    language() {
      return this.$store.state.app.language;
    },
  },
  methods: {
    contentChangeFuncs(e) {
      this.form.policy.service = e;
      localStorage.setItem("contens1", e);
    },
    contentChangeFunc(e) {
      this.form.policy.privacy = e;
      localStorage.setItem("contens", e);
    },
    contentChangechargeFunc(e) {
      this.form.policy.charge = e;
      localStorage.setItem("charge", e);
    },
    contentChangeReadFunc(e) {
      this.form.policy.needred = e;
      localStorage.setItem("needred", e);
    },
    /*获取配置数据*/
    getParams() {
      let self = this;
      SettingApi.storeDetail({}, true)
        .then((res) => {
          self.form = Object.assign(self.form, res.data);
          self.all_type = res.data.allType;
          self.form.policy.charge = res.data.policy.charge
          self.form.policy.needred = res.data.policy.needred
          self.form.policy.privacy = res.data.policy.privacy
          self.form.policy.service = res.data.policy.service

          const serviceEl = UE.getEditor('editor-demo-01')
          if (serviceEl)
            serviceEl.setContent(res.data.policy.service)
          const privacyEl = UE.getEditor('editor-demo-02')
          if (privacyEl)
            privacyEl.setContent(res.data.policy.privacy)
          const chargeEl = UE.getEditor('editor-demo-03')
          if (chargeEl)
            chargeEl.setContent(res.data.policy.charge)
          const needredEl = UE.getEditor('editor-demo-04')
          if (needredEl)
            needredEl.setContent(res.data.policy.needred)

          self.loading = false;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    /*提交*/
    onSubmit() {
      let self = this;
      let params = this.form;

      if (params.deliveryType.length < 1) {
        ElMessage({
          message: "配送方式至少选择一种！",
          type: "warning",
        });
        return;
      }
      console.log('提交数据', params)
      self.loading = false;
      self.$refs.form.validate((valid) => {
        if (valid) {
          console.log('数据', params)
          SettingApi.editStore(params, true)
            .then((data) => {
              self.loading = false;
              ElMessage({
                message: "恭喜你，商城设置成功",
                type: "success",
              });

              self.getParams();
              location.reload();
            })
            .catch((error) => {
              self.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
.tips {
  color: #ccc;
}

.edit_container {
  position: relative !important;
}
</style>
